package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.dao.AppInfoMapper;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.AppInfo;
import com.qfedu.openplateform.manager.entity.Customer;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.AppInfoService;
import com.qfedu.openplateform.manager.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.jar.JarOutputStream;

@RestController
@RequestMapping("/sys/app_info")
public class AppInfoController {

    @Autowired
    private AppInfoService appInfoService;

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/table")
    public TableData table(AppInfo appInfo){
        PageHelper.startPage(1,10);
        List<AppInfo> all = appInfoService.findAll(appInfo);
        PageInfo<AppInfo> pageInfo = new PageInfo<>(all);

        return new TableData(pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping("/del")
    public AjaxMessage del(Integer[] ids){
        int i = appInfoService.delAppInfo(ids);
        AjaxMessage ajaxMessage = new AjaxMessage();

        if (i > 0) {
            ajaxMessage.setStatus(true);
            ajaxMessage.setMessage("删除成功");
        } else {
            ajaxMessage.setMessage("删除成功");
        }
        return ajaxMessage;
    }
    @RequestMapping("/add")
    public AjaxMessage add(AppInfo appInfo) {
        System.out.println(appInfo.getCusId()+"555555555555555555");
        Customer customer = customerService.customerId(appInfo.getCusId());

        System.out.println(customer);
        appInfo.setCorpName(customer.getNickname());
        System.out.println(appInfo);
        int i = appInfoService.insertAppInfo(appInfo);
        AjaxMessage ajaxMessage = new AjaxMessage();
        if (i == 1) {
            ajaxMessage.setStatus(true);
            ajaxMessage.setMessage("添加成功");
        } else {
            ajaxMessage.setMessage("添加失败");
        }
        return ajaxMessage;
    }
    @RequestMapping("/update")
    public AjaxMessage update(AppInfo appInfo) {
        Customer customer = customerService.customerId(appInfo.getCusId());
        System.out.println(customer);

        appInfo.setCorpName(customer.getNickname());
        System.out.println(appInfo);
        int i = appInfoService.updateAppInfo(appInfo);
        AjaxMessage ajaxMessage = new AjaxMessage();
        if (i == 1) {
            ajaxMessage.setStatus(true);
            ajaxMessage.setMessage("修改成功");
        } else {
            ajaxMessage.setMessage("修改失败");
        }
        return ajaxMessage;
    }


}
